using Engine;
using Game;
using GameEntitySystem;
using TemplatesDatabase;

namespace SCIENEW {
    /// <summary>
    /// 在铁水钢水里烫化的组件
    /// </summary>
    public class ComponentMoltenMetalInteract : Component, IUpdateable {
        public UpdateOrder UpdateOrder => UpdateOrder.Default;

        public ComponentCreature m_componentCreature;
        public SubsystemTerrain m_subsystemTerrain;
        public SubsystemTime m_subsystemTime;

        public override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap) {
            base.Load(valuesDictionary, idToEntityMap);
            m_componentCreature = Entity.FindComponent<ComponentCreature>(true);
            m_subsystemTerrain = Project.FindSubsystem<SubsystemTerrain>(true);
            m_subsystemTime = Project.FindSubsystem<SubsystemTime>(true);
        }

        public void Update(float dt) {
            Vector3 position = m_componentCreature.ComponentBody.Position;
            int x = Terrain.ToCell(position.X);
            int y = Terrain.ToCell(position.Y + 0.01f);
            int z = Terrain.ToCell(position.Z);
            if (m_subsystemTerrain.Terrain.GetCellContents(x, y, z) is MoltenIronBlock.Index or MoltenSteelBlock.Index) {
                m_componentCreature.ComponentHealth.Injure(2f * dt, null, false, "被滚烫的金属烫化");
                float num2 = 1.1f + (0.1f * (float)Math.Sin(12.0 * m_subsystemTime.GameTime));
                m_componentCreature.ComponentHealth.m_redScreenFactor = MathUtils.Max( m_componentCreature.ComponentHealth.m_redScreenFactor, num2 * 1.5f);
                m_componentCreature.ComponentHealth.m_componentOnFire.SetOnFire(null, 8f);

                if (m_subsystemTime.PeriodicGameTimeEvent(3, 0)) {
                    Project.FindSubsystem<SubsystemAudio>().PlaySound("Audio/SizzleLong", 1f, 0f, position, 4f, autoDelay: true);
                }
            }
        }
    }
}